#!/bin/bash

# 加载os-release 到环境变量
source /etc/os-release
# - 判断脚本是否适配
[ $(uname -m) != "aarch64" ] && echo '此脚本只适用于armbian的arm架构linux操作系统' && exit 1
# - 如果非空，则声明全局只读变量
test "$ID" && test "$VERSION_CODENAME" && export ID VERSION_CODENAME && readonly ID VERSION_CODENAME

# A-新建管理员
my_user(){
    read -r -p '输入用户名:  ' user_name
    groupadd my_adm
    useradd "${user_name:=lhc}" -G my_adm
    passwd "${user_name}"
    cp -av /etc/sudoers /etc/sudoers."$(date +%s)"
    sed -i -r '$a%my_adm ALL=NOPASSWD:ALL' /etc/sudoers
}

# B-连wifi
my_wifi(){
    nmcli d wifi
    read -r -p '输入wifi名字： ' wifi_name
    read -r -p '输入wifi密码： ' wifi_pass
    nmcli d wifi c "${wifi_name:=wifi_name}" password "${wifi_pass:=wifi_pass}"
}

# C-设置主机名
my_host(){
    hostnamectl status
    read -r -p '输入主机名：   ' my_hostname
    hostnamectl set-hostname "${my_hostname}"
}

# D-关闭防火墙
close_firewall(){
    systemctl disable ufw
    systemctl stop ufw
    systemctl status ufw
}

# E-关闭权限控制
close_apparmor(){
    systemctl disable apparmor
    systemctl stop apparmor
    systemctl status apparmor
}

# F-armbian的ubuntu设置国内源ustc
# 可能还有一些其他源捣乱/etc/apt/sources.list.d/*，可以考虑注释
my_aptlist(){
    [ ${ID} != "ubuntu" ] && echo "${ID}不适配" && return 11
    cp -av /etc/apt/sources.list /etc/apt/sources.list"$(date +%s)"
    cat <<EOF > /etc/apt/sources.list
# 默认注释了源码仓库，如有需要可自行取消注释
deb https://mirrors.ustc.edu.cn/${ID}/ $VERSION_CODENAME main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/${ID}/ $VERSION_CODENAME main restricted universe multiverse

deb https://mirrors.ustc.edu.cn/${ID}/ $VERSION_CODENAME-security main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/${ID}/ $VERSION_CODENAME-security main restricted universe multiverse

deb https://mirrors.ustc.edu.cn/${ID}/ $VERSION_CODENAME-updates main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/${ID}/ $VERSION_CODENAME-updates main restricted universe multiverse

deb https://mirrors.ustc.edu.cn/${ID}/ $VERSION_CODENAME-backports main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/${ID}/ $VERSION_CODENAME-backports main restricted universe multiverse

# 预发布软件源，不建议启用
# deb https://mirrors.ustc.edu.cn/${ID}/ $VERSION_CODENAME-proposed main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/${ID}/ $VERSION_CODENAME-proposed main restricted universe multiverse
EOF
    cat <<EOF > /etc/apt/sources.list.d/docker.list
deb [arch=arm64 signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.ustc.edu.cn/docker-ce/linux/${ID} $VERSION_CODENAME stable
EOF

    apt clean all
    apt update
    apt upgrade
}

# G-armbian的debian设置国内源tuna
my_aptsources(){
    [ ${ID} != "debian" ] && echo "${ID}不适配" && return 11
    cp -av /etc/apt/sources.list.d/debian.sources /etc/apt/sources.list.d/debian.sources"$(date +%s)"
    cp -av /etc/apt/sources.list.d/armbian.sources /etc/apt/sources.list.d/armbian.sources"$(date +%s)"
    sed -i 's#http://deb.debian.org/debian#https://mirrors.ustc.edu.cn/debian#g; s#http://security.debian.org/#https://mirrors.ustc.edu.cn/debian-security#g' /etc/apt/sources.list.d/debian.sources
    sed -i 's#http://apt.armbian.com#https://mirrors.ustc.edu.cn/armbian#g' /etc/apt/sources.list.d/armbian.sources
    cat <<EOF > /etc/apt/sources.list.d/docker.list
deb [arch=arm64 signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.ustc.edu.cn/docker-ce/linux/${ID} $VERSION_CODENAME stable
EOF
    apt clean all
    apt update
    apt upgrade
}

# N-安装vim
# visudo可能默认不是vim，在/etc/sudoers中靠前位置添加
# "Defaults    editor=/usr/bin/vim"
my_vim(){
    apt install -y vim
}

# O-写入公钥
my_pubkey(){
    mkdir -p /root/.ssh/
    touch /root/.ssh/authorized_keys
    echo '
ecdsa-sha2-nistp521 AAAAE2VjZHNhLXNoYTItbmlzdHA1MjEAAAAIbmlzdHA1MjEAAACFBAF/zVnntJPEn+WY4K2Of5cPzlUITu16cQG5dTE6kcn7gdi3owscGbt7hx2ajad8SVU5/V/ZGvlhiFT7/GK4OIP9kQF/jmryW0bVpVVURW5lv0ZZOlJ/CpVMhsE6geauav7vU1xWqY6gSQ3LX3TV74TWwGLsHe+mneYxFdc14f3bAhLI9Q== 521_bit_ssh_key' | tee -a /root/.ssh/authorized_keys
    sed -i -r '/PubkeyAuthentication/d' /etc/ssh/sshd_config &&
    sed -i -r '$aPubkeyAuthentication yes' /etc/ssh/sshd_config
    sed -i -r '/PermitRootLogin/d' /etc/ssh/sshd_config &&
    sed -i -r '$aPermitRootLogin prohibit-password' /etc/ssh/sshd_config
    sshd -t && systemctl restart sshd
}


echo "
A-新建管理员
B-连wifi
C-设置主机名
D-关闭防火墙
E-关闭权限控制
F-armbian的ubuntu设置国内源ustc
G-armbian的debian设置国内源
N-安装vim
O-写入公钥
"
# 收集选项
read -r -p '输入要执行的行首字母(例如ABC)：' my_select
# 去掉非字母数字项，转为数组保存
my_arr=($(echo "$my_select" | tr -cd 'A-Za-z0-9' | fold -w1))
# 数组去重
my_arr=($(printf "%s\n" "${my_arr[@]}" |sort |uniq))
for i in "${my_arr[@]}"
do
    case "$i" in 
        A)
            # A-新建管理员
            my_user
            ;;
        B)
            # B-连wifi
            my_wifi
            ;;
        C)
            # C-设置主机名
            my_host
            ;;
        D)
            # D-关闭防火墙
            close_firewall        
            ;;
        E)
            # E-关闭权限控制
            close_apparmor        
            ;;
        F)
            # F-armbian的ubuntu设置国内源ustc
            my_aptlist        
            ;;
        G)  
            # G-armbian的debian设置国内源tuna
            my_aptsources        
            ;;
        N)
            # N-安装vim
            my_vim        
            ;;
        O)
            # O-写入公钥
            my_pubkey        
            ;;
        0)
            declare -F
            ;;
        *)
            echo "部分内容未匹配 $i"
            ;;
    esac
done
